<?php

namespace App\Http\Controllers;

use App\Http\Model\Admin;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;

class AdminController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()//管理员列表页方法
    {
        $data = Admin::all();//查管理员admin数据表的全部信息
        return view('home/admin/index')->with('data',$data);//返回信息到页面
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()//添加管理员显示方法
    {
        return view('home/admin/create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)//添加管理员操作方法
    {
        $input = Input::except('_token');//获取异步传过来的数据
        $input['password'] = Crypt::encrypt('123456');//添加账户密码默认为123456 crypt方法加密处理
        $input['state'] = 1;
        $res = Admin::create($input);//写入admin账户表
        if($res){//判断写入是否成功
            return response()->json(['code'=>1,'msg'=>'登记成功' ],200);
        }else{
            return response()->json(['code'=>0,'msg'=>'登记失败' ],200);
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)//修改账户页面显示
    {
        $data = Admin::where('id','=',$id)->first();//查找要修改的账号信息
        return view('home/admin/edit')->with('data',$data);//数据返回到页面
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)//修改账号操作方法
    {
        $input = Input::except('_method','_token');
        $res = DB::table('admin')->where('id','=',$id)->update($input);//修改账号表
        if($res){
            return response()->json(['code'=>1,'msg'=>'修改成功' ],200);
        }else{
            return response()->json(['code'=>0,'msg'=>'修改失败' ],200);
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }

    public function able(){//账号的启用禁用操作方法
        $input = Input::all();
        $res = DB::table('admin')->where('id','=',$input['aid'])->update(['state'=>$input['state']]);//修改admin表中的state状态值
        if($res){
            return response()->json(['code'=>1,'msg'=>'操作成功' ],200);
        }else{
            return response()->json(['code'=>0,'msg'=>'操作失败' ],200);
        }
    }
}
